<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/31
 * Time: 14:20
 */

namespace Admin\Controller;

use Bbs\Api\ForumAdmingroupApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumThreadApi;
use Bbs\Api\ForumPostApi;
use Admin\Api\MemberApi;
use Bbs\Api\ForumUserAdmingroupApi;
use Ucenter\Api\UcenterMemberApi;
use Uclient\Api\UserApi;

class BbsForumController extends AdminController{

    //添加板块
    public function add(){
        if(IS_GET){

            //查询论坛列表
            $result = apiCall(ForumForumApi::QUERY_NO_PAGING);
            if($result['status']){
                $this->assign('forum_list',$result['info']);
            }else{
                $this -> error($result['info']);
            }

            $this -> display();
        }else{
            $fup          = I('post.fup',0,'int');
            $name         = I('post.name','');
            $icon_id      = I('post.icon_id','');
            $displayorder = I('post.displayorder','');
            $description  = I('post.description','');
            $modnewposts  = I('post.modnewposts','');
            if(empty($name)) $this->error('论坛名称不能为空');
            if($fup){
                $result = apiCall(ForumForumApi::GET_INFO,array(array('fid'=>$fup,'fup'=>0)));
                if(!$result['status'])  $this->error('论服务器故障：forum get');
                if(empty($result['info'])) $this->error('不能再添加子板块了');
            }
            $entity = array(
                'name'           => $name,
                'icon_id'        => $icon_id,
                'displayorder'   => $displayorder,
                'description'    => $description,
                'modnewposts'    => $modnewposts,
                'fup'            => $fup,
                'yesterdayposts' => 0,
                'todayposts'     => 0,
                'status'         => 0,
                'lastpost'       => '',
            );
            $result = apiCall(ForumForumApi::ADD,array($entity));
            if($result['status']){
                $this -> success("添加成功",U('Admin/BbsForum/index',array('fup'=>$fup)));
            }else{
                $this -> error($result['info']);
            }
        }
    }

    /**
     * 板块统计 - 包括子论坛 - 不统计删除状态
     * @return [type] [description]
     */
    public function count(){
        $result = apiCall(ForumForumApi::QUERY_WITH_CHILD);
        if(!$result['status']) $this -> error("api error：forum query child");
        if(is_array($result['info'])){
            $forum = array();
            $list  = $result['info'];
            //数组 重新组合 以fid为键名
            foreach ($list as $v) {
                $forum[$v['fid']] = array('fid'=>$v['fid'],'fup'=>$v['fup'],'threads'=>$v['threads'],'posts'=>$v['posts'],'child'=>array());
                if(is_array($v['child'])){
                    foreach ($v['child'] as $k) {
                        $forum[$v['fid']]['child'][$k['fid']] = array('fid'=>$k['fid'],'fup'=>$k['fup'],'threads'=>$k['threads'],'posts'=>$k['posts'],'child'=>array());;
                    }
                }
            }
            // dump($forum);
            foreach ($forum as $k => &$v) {
                if(empty($v['child'])){
                    $result = apiCall(ForumThreadApi::COUNT,array(array('fid'=>$k,'status'=>array('neq',-1))));
                    if(!$result['status']) $this -> error("api thread count");
                    $v['cthreads'] = intval($result['info']);
                    $result = apiCall(ForumPostApi::COUNT,array(array('fid'=>$k,'status'=>array('neq',-1))));
                    if(!$result['status']) $this -> error("api post count");
                    $v['cposts'] = intval($result['info']);
                }else{
                    $v['cthreads'] = 0;
                    $v['cposts']   = 0;
                    foreach ($v['child'] as $kk => &$vv) {
                        $result = apiCall(ForumThreadApi::COUNT,array(array('fid'=>$kk,'status'=>array('neq',-1))));
                        if(!$result['status']) $this -> error("api thread count");
                        $vv['cthreads'] = intval($result['info']);
                        $v['cthreads']  = $v['cthreads']+$vv['cthreads'];
                        $result = apiCall(ForumPostApi::COUNT,array(array('fid'=>$kk,'status'=>array('neq',-1))));
                        if(!$result['status']) $this -> error("api post count");
                        $vv['cposts'] = intval($result['info']);
                        $v['cposts']  = $v['cposts']+$vv['cposts'];
                        if($vv['cthreads'] != $vv['threads'] || $vv['cposts'] != $vv['posts']){
                            $result = apiCall(ForumForumApi::SAVE,array(array('fid'=>$kk),array('threads'=>$vv['cthreads'],'posts'=>$vv['cposts'])));
                            if(!$result['status']) $this -> error("api forum save");
                        }
                    }
                }
                if($v['cthreads'] != $v['threads'] || $v['cposts'] != $v['posts']){
                    $result = apiCall(ForumForumApi::SAVE,array(array('fid'=>$k),array('threads'=>$v['cthreads'],'posts'=>$v['cposts'])));
                    if(!$result['status']) $this -> error("api forum save");
                }
            }
            // dump($forum);
            $this -> success("操作成功");
        }else{
            $this -> error($result['info']);
        }
    }
    /**
     *
     */
    public function admin(){
        if(IS_GET){
            $fid = I("get.fid",'');
            if($fid == "" || $fid <= 0){
                $this -> error("参数错误");
            }
            $this -> assign('fid',$fid);
            $result = apiCall(ForumForumApi::GET_INFO,array(array('fid'=>$fid)));
            if(!$result['status']){
                $this -> error($result['info']);
            }
            $this -> assign('fname',$result['info']['name']);

            $map = array(
                'fid' => $fid,
                'adminid' => 2,
            );
            $result = apiCall(ForumUserAdmingroupApi::QUERY_NO_PAGING,array($map));
            if(!$result['status']){
                $this -> error($result['info']);
            }
            //查询用户信息
            foreach($result['info'] as &$vo){

                $res = apiCall(UserApi::GET_INFO,array($vo['uid']));

                if(!$res['status'] || $res['info'] == ""){
                    $this -> error($res['info']);
                }

                $vo['username'] = $res['info']['username'];

            }
            $this -> assign('list',$result['info']);

            $this -> display();
        }
    }

    public function delAdmin(){
        if(IS_POST){
            $uid = I('post.uid','');
            $fid = I('post.fid','');
            if($fid == '' || $uid == '' || $uid <= 0 || $fid <= 0){
                $this -> error("参数有误");
            }
            $map = array(
                'uid' => $uid,
                'fid' => $fid,
                'adminid' => 2,
            );
            $result = apiCall(ForumUserAdmingroupApi::DELETE,array($map));
            if($result['status']){
                if($result['info'] == 0){
                    $this -> error('信息错误');
                }else{
                    $this -> success('操作成功');
                }
            }else{
                $this -> error($result['info']);
            }
        }
    }

    public function addAdmin(){
        if(IS_POST){
            $fid = I('post.fid','');
            $uid = I('post.uid','');
            if($fid == '' || $uid == '' || $fid <= 0 || $uid <= 0){
                $this -> error("参数错误");
            }
            $map = array(
                'uid' => $uid,
            );
            $result = apiCall(ForumUserAdmingroupApi::GET_INFO,array($map));
            if($result['status']){
                if($result['info'] != ""){
                    $this -> error("该用户以有其他管理身份或者已被禁言！");
                }
            }else{
                $this -> error($result['info']);
            }
            $entity = array(
                'fid' => $fid,
                'uid' => $uid,
                'adminid' => 2,
            );
            $result = apiCall(ForumUserAdmingroupApi::ADD,array($entity));
            if($result['status']){
                $this -> success("操作成功");
            }else{
                $this -> error($result['info']);
            }
        }
    }

    //板块管理 - 首页 - 仅2级
    public function index(){

        if(IS_GET){
            //TODO 查询超级版主
            $fup = I('fup',0,'int');
            $map = array('fup' => $fup);
            $page = array('curpage'=>I('p'),'size'=>10);
            $order = "displayorder desc";
            $result2 = apiCall(ForumForumApi::QUERY,array($map,$page,$order));
            if($result2['status']){
                $list2 = $result2['info']['list'];
                foreach ($list2 as &$v) {
                    $result = apiCall(MemberApi::GET_FORUM_ADMIN,array($v['fid'],2));
                    $v['admin'] = $result;
                    if(0 === $fup){
                        $map = array('fup'=>$v['fid']);
                        $result = apiCall(ForumForumApi::QUERY_NO_PAGING,array($map,$order));
                        if($result['status']){
                            $list = $result['info'];
                            foreach ($list as &$vv) {
                                $result = apiCall(MemberApi::GET_FORUM_ADMIN,array($vv['fid'],2));
                                $vv['admin'] = $result;
                            }
                            $v['child'] = $list;
                        }
                    }
                }
                // dump($list2);exit;
                $this -> assign('list',$list2);
                $this -> assign('show',$result2['info']['show']);
            }else{
                $this -> error($result['info']);
            }

            $this -> display();
        }

    }
    public function status(){
        $fid    = I('get.fid','-1','int');
        $status = I('get.status','-2','int');
        if(!in_array($status,array(-1,0,1))){
            $this->error('参数有误');
        }
        $result = apiCall(ForumForumApi::SAVE,array(array('fid'=>$fid),array('status'=>$status)));
        if($result['status']) $this->success('修改成功');
        $this->error('修改失败');
    }
    //审核开关
    public function modnewposts(){

        $fid = I('post.fid','');
        $modnewposts = I('post.modnewposts','');
        //数据合法性验证
        if($modnewposts != 0 && $modnewposts != 1){
            $this -> error('参数有误');
        }
        $map = array(
            'fid' => $fid,
        );
        $result = apiCall(ForumForumApi::GET_INFO,array($map));
        if($result['status']){
            if($result['info']['modnewposts'] == $modnewposts){
                if($modnewposts == 0){
                    $this -> error("该板块未开启审核");
                }else if($modnewposts == 1){
                    $this -> error("该板块已开启审核");
                }
            }
        }else{
            $this -> error($result['info']);
        }

        $map = array(
            'fid' => $fid,
        );
        $entity = array(
            'modnewposts' => $modnewposts,
        );
        $result = apiCall(ForumForumApi::SAVE,array($map,$entity));
        if($result['status']){
            if($modnewposts == 0){
                $this -> success("取消审核成功",U("Admin/BbsForum/index"));
            }else if($modnewposts == 1){
                $map = array('fid' => $fid);
                $result = apiCall(ForumPostApi::SAVE,array($map,array('invisible' => 1)));
                if($result['status']){
                    $this -> success("开启审核成功",U("Admin/BbsForum/index"));
                }else{
                    $this->error($result['info']);
                }
            }
        }else{
            $this -> error($result['info']);
        }

    }

    public function edit(){

        if(IS_GET){

            $fid = I('get.fid','');
            if($fid == ''){
                $this -> error("参数错误");
            }
            $map = array(
                'fid' => $fid,
            );
            $this -> assign('fid',$fid);
            $result = apiCall(ForumForumApi::GET_INFO,array($map));
            if($result['status']){
                $this -> assign('name',$result['info']['name']);
                $this -> assign('icon_id',$result['info']['icon_id']);
                $this -> assign('displayorder',$result['info']['displayorder']);
                $this -> assign('threads',$result['info']['threads']);
                $this -> assign('posts',$result['info']['posts']);
                $this -> assign('tadayposts',$result['info']['todayposts']);
                $this -> assign('yesterdayposts',$result['info']['yesterdayposts']);
                $this -> assign('description',$result['info']['description']);
                $this -> assign('modnewposts',$result['info']['modnewposts']);
                $this -> assign('fup',$result['info']['fup']);
            }else{
                $this -> error($result['info']);
            }
            //查询论坛列表
            $result = apiCall(ForumForumApi::QUERY_NO_PAGING);
            if($result['status']){
                $this->assign('forum_list',$result['info']);
            }else{
                $this -> error($result['info']);
            }

            $this -> display();
        }else if(IS_POST){

            $fup = I('post.fup',0,'int');
            $fid = I('post.fid','');
            $name = I('post.name','');
            $icon_id = I('post.icon_id','');
            $displayorder = I('post.displayorder','');
            $description = I('post.description','');
            $modnewposts = I('post.modnewposts','');

            if(empty($name)){
                $this->error('论坛名称不能为空');
            }

            $entity = array(
                'name' => $name,
                'icon_id' => $icon_id,
                'displayorder' => $displayorder,
                'description' => $description,
                'modnewposts' => $modnewposts,
                'fup' => $fup
            );

            $map = array(
                'fid' => $fid,
            );

            $result = apiCall(ForumForumApi::SAVE,array($map,$entity));

            if($result['status']){
                $this -> success("修改成功!");
            }else{
                $this -> error($result['info']);
            }

        }

    }

}